<?php

namespace Admin\Controller;

use \Org\Util\Rest;

class TestController extends AdminController {

    public function test(){
        //rest请求
        //取数据
        //存放到数组
        //过滤一遍数组
        //调用回调函数处理数据
        //返回处理完成的数据

        $rest = new Rest();
        $where = json_encode(array("versionCode"=>1));
        $res = $rest->getList('Version',$where);

        $list = $res->body->results;

        $list = $this->pullParse('Version',
            array('id'=>'objectId','client'=>'client','versionName'=>'versionName'),
            function($src){
                $src['data'] = 'a';
                return $src;
            },
            function($src){
                echo $src['data'];
                $autoid = $src['autoid'];
                $versionName = $src['versionName'];
                $m = M('t');
                $flag = $m->add(array('refid'=>$autoid,'val'=>$versionName));

                return $src;
            },
            $list);
        echo "<hr/>";
        dump($list);
    }

    public function pullParse($table,$map,$before,$after,$array){

        $map['_table'] = $table;
        $map['_before'] = $before;
        $map['_after'] = $after;
        return array_map(function($src,$map){
            $table = $map['_table'];
            $before = $map['_before'];
            $after = $map['_after'];
            unset($map['_table']);
            unset($map['_after']);
            unset($map['_before']);
            $src = (array)$src;
            $row = array();
            if(count($map)>0){
                foreach($map as $k=>$v){
                    $row[$k] = $src[$v];
                }
            }else{
                $row = $src;
            }

            $row = call_user_func($before,$row);
            $m = M($table);
            $id = $m->add($row);
            $row['autoid'] = $id;
            $row = call_user_func($after,$row);
            return $row;
        },$array,array_fill(0,count($array),$map));

    }

    public function pushParse($class,$map,$before,$after,$array){

        $map['_class'] = $class;
        $map['_before'] = $before;
        $map['_after'] = $after;
        return array_map(function($src,$map){
            $class = $map['_class'];
            $before = $map['_before'];
            $after = $map['_after'];
            unset($map['_class']);
            unset($map['_after']);
            unset($map['_before']);

            $src = call_user_func($before,$src);
            $row = array();
            foreach($map as $k=>$v){
                $row[$k] = $src[$v];
            }

            //上推代码

            $rest = new Rest();
            /**
             * 0: 默认
            1：新增
            2：修改
            3：删除
             */
            switch($src['syncStatus']){
                case 1:
                    $rst = $rest->add($class,json_encode($row));
                    break;
                case 2:
                    $rst = $rest->put($class,$row['objectId'],json_encode($row));
                    break;
                case 3:
                    $rst = $rest->delete($class,$row['objectId']);
                    break;
            }
            $row['flag'] = $rst;
            $row = call_user_func($after,$row);
            return $row;
        },$array,array_fill(0,count($array),$map));

    }

    public function test1(){

        //取数据
        //存放到数组
        //过滤一遍数组
        //调用回调函数处理数据
        //rest请求
        //返回处理完成的数据

        $m = M('version');

        $list = $m->where(array('syncStatus'=>2))->select();

        $list = $this->pushParse('Version',
            array('objectId'=>'autoid','client'=>'client','versionCode'=>'versionCode','versionName'=>'versionName'),
            function($src){
                $refid = $src['autoid'];
                $m = M('t');
                $d = $m->where(array('refid' =>"$refid"))->find();
                $src['versionName'] = $d['val'];
                $src['versionCode'] = intval($src['versionCode']);
                return $src;
            },
            function($src){
                $m = M('Version');
                $m->where('autoid='.$src['objectId'])->data(array('syncStatus'=>0))->save();
                echo $m->getLastSql();
                return $src;
            },
            $list);
        echo "<hr/>";
        dump($list);
    }

    /*
     * {"address":"dafda","updatedAt":"2015-06-27T03:08:31.458Z",
     * "isDefault":true,"phone":"111","uid":"558a2e9be4b0511ccaee347c",
     * "name":"sadf","objectId":"558cbeeae4b060308e1cb22d",
     * "city":"扬州市","createdAt":"2015-06-26T02:54:34.584Z",
     * "area":"广陵区",
     * "province":"江苏省"}
     */

    /**
     * 下拉团购订单信息
     */
    public function pullGroupOrders(){
        //
        //下载远程订单数据
        $rest = new Rest();
//        $where = json_encode(array("versionCode"=>1));
        $res = $rest->getList('GroupPurchaseOrder');

        $list = $res->body->results;
//        var_dump($list);

        $this->pullParse("order",
            array(),
            function($src){
                $address = (array)($src['address']);
                $src['toPhone'] = $address['phone'];

                return $src;
            },
            function($src){
                $goods = $src['goods'];
                $list = array();
                foreach($goods as $g){
                    $g = (array)$g;
                    $list[] = array('orderid' => $g['orderid'],
                        'pid'=>$g['sid'],'cartnum'=>$g['size'],'createdate'=>0,'lastupdate'=>0,'syncStatus'=>1);
                }
                var_dump($list);
                $m = M('order_goods');
                $flag = $m->addAll($list);
                $src['goodsFlag'] = $flag;
//                $autoid = $src['autoid'];
//                $versionName = $src['versionName'];
//                $m = M('t');
//                $flag = $m->add(array('refid'=>$autoid,'val'=>$versionName));

                return $src;
            },
            $list);
    }
}
